package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.FileInputStream;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import AStarPackage.AStar;
import Action.BaseTool;
import Action.MagicEffectList;
import CreateObject.HeroList1;
import CreateObject.StoneList;
import Object.Direction_Type;
import Object.Hero;
import Object.MagicEffect;
import Object.Player;
import Object.Point;
import Object.Terrain;



public class Shot1 extends JFrame implements MouseListener, KeyListener {

    //引入自定义工具
    BaseTool bt = new BaseTool();

    //创建英雄
    HeroList1 hlist = new HeroList1();
    Hero h1 = hlist.Hero4();
    Hero h2 = hlist.Hero5();
    Player p;

    //创建选中特效
    MagicEffectList me = new MagicEffectList();
    MagicEffect select = me.select();
    int M[][] = new int[12][12];

    int endX = 0;
    int endY = 0;

    public Shot1() throws Exception {
        this.setSize(1400,800);
        this.setVisible(true);
        this.setLayout(null);
        this.setTitle("射击测试");
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置关闭方式

        this.repaint();
        this.addMouseListener(this);
        this.addKeyListener(this);


        p = new Player(); //创建玩家
        initGame();//初始化内容
    }

    public void initGame() throws Exception {
        M = BaseTool.getFMap("素材//地图//Map1.txt");//
        M[2][2] = 2; M[6][10] = 3;
        h1.setloc(2, 2);
        h2.setloc(6, 10);

    }

    boolean select1 = false;
    boolean select2 = false;
    boolean control1 = false;
    boolean control2 = false;

    public void paint(Graphics g) {
        //双缓冲技术
        BufferedImage buf = new BufferedImage(1400,1200, BufferedImage.TYPE_INT_RGB);

        Graphics g1 =  buf.createGraphics();  // 创建画笔
        Graphics g2 =  buf.createGraphics();

        g2.setColor(new Color(255,255,255));
        g2.setFont(new Font("宋体",Font.BOLD,25));

        ImageObserver observer = this;

        MouseAdapter mouseAdapter = new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e)	{
                if(e.getX()/50 == h1.getpoint().getX() && e.getY()/50 == h1.getpoint().getY()) {
                    select1 = true;
                    repaint();
                }else {
                    select1 = false;
                    repaint();
                }

                if(e.getX()/50 == h2.getpoint().getX() && e.getY()/50 == h2.getpoint().getY()) {
                    select2 = true;
                    repaint();
                }else {
                    select2 = false;
                    repaint();
                }
                g1.drawImage(select.getImage(),e.getX()/50,e.getX()/50,50,50,observer);
                repaint();
            }
        };
        addMouseMotionListener(mouseAdapter);//创建鼠标移动监听

        //选中情况
        if(select1 || control1) {
            g1.drawImage(select.getImage(),h1.getpoint().getX()*50,h1.getpoint().getY()*50,50,50,this);
            g2.drawString(h1.getName(),700,150);
            g2.drawString(h1.show0(),700,190);
            g2.drawString(h1.showLife(),700,230);
            g2.drawString(h1.show1(),700,270);
            g2.drawString(h1.show2(),700,310);
            g2.drawString(h1.show3(),700,350);
        }
        if(select2 || control2) {
            g1.drawImage(select.getImage(),h2.getpoint().getX()*50,h2.getpoint().getY()*50,50,50,this);
            g2.drawString(h2.getName(),700,150);
            g2.drawString(h2.show0(),700,190);
            g2.drawString(h2.showLife(),700,230);
            g2.drawString(h2.show1(),700,270);
            g2.drawString(h2.show2(),700,310);
            g2.drawString(h2.show3(),700,350);
        }

        //绘制物体
        Terrain stone = new Terrain();
        StoneList co = new StoneList();
        stone = co.Stone1();
        for(int i=0;i<12;i++) {
            for(int j=0;j<12;j++) {
                if(M[i][j]==1) {
                    g1.drawImage(stone.getImage(),i*50,j*50,50,50,this);
                }
                if(M[i][j]==2) {
                    g1.drawImage(h1.getImage(),h1.getpoint().getX()*50,h1.getpoint().getY()*50,50,50,this);
                }
                if(M[i][j]==3) {
                    g1.drawImage(h2.getImage(),h2.getpoint().getX()*50,h2.getpoint().getY()*50,50,50,this);
                }
            }
        }

        //页面区域框
        for (int i = 0; i <= 13; i++) {
            g1.setColor(Color.white);
            g1.drawLine(0, i*50, 650, i*50);  //画棋盘横线
            g1.drawLine(i*50, 0, i*50, 650);  //画棋盘竖线
        }
        g1.drawRect(800,50,150,50);
        if(control1 || control2) {
            g2.drawString("控制移动中",800,80);
        }
        g1.drawRect(700,120,600,400);

        g.drawImage(buf, 0, 0,this);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub
        if(e.getX()<=800 && e.getX()>=50) {
            if(control1) {
                endX = e.getX()/50;
                endY = e.getY()/50;
                Move(endX,endY,h1);
            }

            if(e.getX()/50 == h1.getpoint().getX() && e.getY()/50 == h1.getpoint().getY()) {
                control1 = true;
                control2 = false;
                repaint();
            }else {
                control1 = false;
                repaint();
            }

            if(control2) {
                endX = e.getX()/50;
                endY = e.getY()/50;
                Move(endX,endY,h2);
            }

            if(e.getX()/50 == h2.getpoint().getX() && e.getY()/50 == h2.getpoint().getY()) {
                control2 = true;
                control1 = false;
                repaint();
            }else {
                control2 = false;
                repaint();
            }
        }
        else {
            control1 = false;
            control2 = false;
        }
    }

    private boolean Move(int endX2, int endY2, Hero H) {

        if((endX2<12 && endX2>=0) && (endY2<12 && endY2>=0) ) {
            if(M[endX2][endY2]==0) {
                //创建A星对象
                AStar astar = new AStar(M,new Point(H.getpoint().getX(),H.getpoint().getY()),new Point(endX2,endY2));
                //开始寻路
                ArrayList<Point> pointList = astar.start();

                class Work extends Thread {
                    @Override
                    public void run() {
                        try {
                            if(pointList!=null) {
                                for(Point point:pointList){
                                    //打印寻路结果
                                    //System.out.println(point.getX()+","+ point.getY());

                                    int x = point.getX();
                                    int y = point.getY();
                                    M[H.getpoint().getX()][H.getpoint().getY()] = 0;//将地图上的该英雄移走
                                    H.setloc(x,y);//设置英雄的新位置

                                    //将新的位置上放上对应英雄
                                    if(x==h1.getpoint().getX() && y==h1.getpoint().getY()) {
                                        M[x][y] = 2;
                                    }
                                    if(x==h2.getpoint().getX() && y==h2.getpoint().getY()) {
                                        M[x][y] = 3;
                                    }
                                    repaint();
                                    sleep(100);
                                }
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
                Work work = new Work();//创建线程使移动连贯
                work.start();

		        /*
		        if(pointList!=null) {
		            int x = pointList.get(pointList.size()-1).getX();
		            int y = pointList.get(pointList.size()-1).getY();
		            M[H.getpoint().getX()][H.getpoint().getY()] = 0;
		            H.setloc(x,y);
		            M[x][y] = 2;
		        }*/
            }
        }else {
            return false;
        }
        //repaint();
        return true;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }
}